(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.dynaDisc.invoiceFilterController', createController);
	createController.$inject = [ '$scope', '$filter', 'Constants', 'arithService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'dynaDiscService' ];
	function createController($scope, $filter, Constants, arithService, modalService, gridService, layoutConfig, validatorConfig, authService, dynaDiscService) {
		var ctrl = this, piDiscountRequestInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};

		ctrl.init = function() {
			ctrl.piDiscountRequest = piDiscountRequestInfo;
			ctrl.dateDisabled = dynaDiscService.dateDisabled;
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.invoiceCriteria.list';
			ctrl.gridOptions.multiSelect = true;
			piDiscountRequestInfo.invoiceCriteria = ctrl.gridOptions.defaultSearchCriteria;
			piDiscountRequestInfo.invoiceCriteria.storageSearchCriteria = false;
			ctrl.invoiceCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.invoiceCriteria.storageSearchCriteria = false;
			
			authService.search(ctrl.invoiceCriteria).then(function(response) {
			dynaDiscService.getCurrencyOptions().then(function(currencyResponse) {
				ctrl.invoiceCcyOptions = currencyResponse.data;
				ctrl.invoiceCcyChanged =function(){
					ctrl.invoiceCriteria.invoiceCcy = ctrl.piDiscountRequest.invoiceCcy;
				};
				ctrl.effectiveDateChanged =function(value, formCtrl, isInitialized){
					if(!isInitialized){
						ctrl.invoiceCriteria.effectiveDate = value.effectiveDate;
						ctrl.search();
					}
				};
			});
			ctrl.selectionStrategyOptions = dynaDiscService.getSelectionStrategyOptions();
			ctrl.rateTypeOptions = dynaDiscService.getRateTypeOptions();
			
			ctrl.piDiscountRequest.invoiceCcy = $scope.site.baseCcy;
			ctrl.invoiceCriteria.invoiceCcy = $scope.site.baseCcy;
			if (response.data && response.data.model) {

				var invoiceCriteria = response.data.model.invoiceCriteria;
				angular.forEach(invoiceCriteria.list, function(data) {
					if(data.communityCodeQ != null && data.communityCode != null){
						data.community = data.communityCodeQ + ":" + data.communityCode;
					}
				});
				angular.merge(ctrl.invoiceCriteria, invoiceCriteria);
				ctrl.piDiscountRequest = response.data.model;
				ctrl.piDiscountRequest.availableAmount = invoiceCriteria.availableAmount;
				ctrl.piDiscountRequest.bizDate = invoiceCriteria.bizDate;
				gridService.updateGridOptions(ctrl.invoiceCriteria, ctrl.gridOptions);
			  }
			},angular.noop);
		};
		
		ctrl.search = function() {
			ctrl.invoiceCriteria.list = [];
			ctrl.invoiceCriteria.totalFinanceAmt = ctrl.piDiscountRequest.totalFinanceAmt;
			ctrl.invoiceCriteria.selectionStrategy = ctrl.piDiscountRequest.selectionStrategy;
			authService.search(ctrl.invoiceCriteria).then(
			function(response) {
				var invoiceCriteria = response.data.model.invoiceCriteria;
				angular.forEach(invoiceCriteria.list, function(data) {
					if(data.communityCodeQ != null && data.communityCode != null){
						data.community = data.communityCodeQ + ":" + data.communityCode;
					}
				});
				angular.merge(ctrl.invoiceCriteria, invoiceCriteria);
				ctrl.piDiscountRequest.availableAmount = invoiceCriteria.availableAmount;
				gridService.updateGridOptions(ctrl.invoiceCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.invoiceCriteria.pageNum = pageNum;
			ctrl.invoiceCriteria.pageSize = pageSize;
			ctrl.invoiceCriteria.list = [];
			ctrl.search();
		}

		ctrl.createPreview = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			ctrl.piDiscountRequest.discountInvoiceList = [];
			ctrl.piDiscountRequest.amtChangedInv = ctrl.amtChangedInv;
			angular.forEach(ctrl.selectedRows, function(invoice, index) {
				ctrl.piDiscountRequest.discountInvoiceList.push({'invoiceId' : invoice.invoiceId});
			});
			return authService.createPreview(ctrl.piDiscountRequest).then(successful, failed);
		};

		function failed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function successful(response) {
			if (response.data && response.data.model) {
				angular.extend(ctrl.piDiscountRequest, response.data.model);
				
				var params = {
						'selectedRows' : ctrl.selectedRows,
						'piDiscountRequest' : ctrl.piDiscountRequest
				};
				$scope.go('apf.dynaDisc.createPreview',params);
			}
		}
		
		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.invoiceCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.invoiceCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
	}
})(angular);